package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsShopApply;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.QualificationConfig;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.PhpMerchantsApplyReq;
import com.example.xpvideo.entity.req.QualificationConfigReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.PHPMerchantsApplyListRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.QualificationConfigMapper;
import com.example.xpvideo.service.QualificationConfigService;
import com.example.xpvideo.service.TimeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 资质配置表(QualificationConfig)Service层
 *
 * @author 杨丽辉
 * @since 2021-09-03 16:19:24
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class QualificationConfigServiceImpl implements QualificationConfigService {
        @Autowired
        private QualificationConfigMapper qualificationConfigMapper;
        @Autowired
        private TimeService timeService;
    @Override
    public ResponseResult<PageResult<List<QualificationConfig>>> list(QualificationConfigReq qualificationConfigReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String name = qualificationConfigReq.getQualificationName();
        Integer IsMust = qualificationConfigReq.getIsMust();
        Integer isLocalMust = qualificationConfigReq.getIsLocalMust();

        PageHelper.startPage(page, size);
        List<QualificationConfig> list = qualificationConfigMapper.selectList(Wrappers.<QualificationConfig>query()
                .eq(!ObjectUtils.isEmpty(isLocalMust), "is_local_must", isLocalMust)
                .eq(!ObjectUtils.isEmpty(IsMust),"is_must",IsMust)
                .like(!ObjectUtils.isEmpty(name),"qualification_name",name)
                .eq("is_del", 1));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<QualificationConfig>> listPageResult = new PageResult<>(count, pages,
                pageSize, pageNum,list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> delete (UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        if(id == 1 || id == 2 || id == 3){
            throw new ErrorException(ErrCodeEnum.unknown_10015);
        }
        QualificationConfig qualificationConfig = new QualificationConfig();

        qualificationConfig.setId(id);
        qualificationConfig.setIsDel(stage);
        qualificationConfig.setUpdateTime(timeService.getTime());
        int res = qualificationConfigMapper.updateQualificationConfig(qualificationConfig);

        if(res > 0){
            return ResponseResult.responseResult(0, "删除成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> insert(QualificationConfig qualificationConfig) throws ErrorException{
        String name = qualificationConfig.getQualificationName();
        Integer isMust = qualificationConfig.getIsMust();
        Integer isLocalMust = qualificationConfig.getIsLocalMust();

        QualificationConfig qualificationConfig1 = qualificationConfigMapper.selectOne(Wrappers.<QualificationConfig>query().eq("qualification_name",name));
        if(!ObjectUtils.isEmpty(qualificationConfig1)){
            throw new ErrorException(ErrCodeEnum.unknown_10016);
        }
        QualificationConfig qualificationConfig2 = new QualificationConfig();
        qualificationConfig2.setQualificationName(name);
        qualificationConfig2.setIsMust(isMust);
        qualificationConfig2.setIsLocalMust(isLocalMust);
        qualificationConfig2.setIsDel(1);
        qualificationConfig2.setCreateTime(timeService.getTime());
        int res = qualificationConfigMapper.insertQualificationConfig(qualificationConfig2);

        if(res > 0){
            return ResponseResult.responseResult(0,"添加资质成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }

    @Override
    public ResponseResult<Object> update(QualificationConfig qualificationConfig) throws ErrorException{
        Integer id = qualificationConfig.getId();
        String name = qualificationConfig.getQualificationName();
        Integer isMust = qualificationConfig.getIsMust();
        Integer isLocalMust = qualificationConfig.getIsLocalMust();

        if(id == 1 || id == 2 || id == 3){
            throw new ErrorException(ErrCodeEnum.unknown_10015);
        }


        QualificationConfig qualificationConfig2= qualificationConfigMapper.selectOne(Wrappers.<QualificationConfig>query().eq("qualification_name",name));
        if(!ObjectUtils.isEmpty(qualificationConfig2)){
            throw new ErrorException(ErrCodeEnum.unknown_10016);
        }

        QualificationConfig qualificationConfig1 = new QualificationConfig();
        qualificationConfig1.setId(id);
        qualificationConfig1.setQualificationName(name);
        qualificationConfig1.setIsMust(isMust);
        qualificationConfig1.setIsLocalMust(isLocalMust);
        qualificationConfig1.setIsDel(1);
        qualificationConfig1.setUpdateTime(timeService.getTime());
        int res = qualificationConfigMapper.updateQualificationConfig(qualificationConfig1);

        if(res > 0){
            return ResponseResult.responseResult(0,"修改资质成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20002);
    }
}

